
#include <string.h>
#include <netdb.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_mac.h"

#include "lwip/err.h"
#include "lwip/sys.h"
#include "esp_netif.h"

#include "softAP.h"
#include "http_server.h"

static const char *TAG = "soft-ap";

char AP_NAME[20] = "DoNotWork-AP-xxxx";
char LogalAP[] = "192.168.6.1";
static uint8_t local_ip[4];
uint8_t base_wifi_mac_addr[6];

esp_netif_t *softap= NULL;
static void softap_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
	uint8_t pre_wifi_state = 0;
	int buf_len = 32;
	int int_ip = 0;

	if (WIFI_EVENT == event_base && WIFI_EVENT_STA_START == event_id) {
		// esp_wifi_connect();
	} else if (WIFI_EVENT == event_base && WIFI_EVENT_STA_DISCONNECTED == event_id) {
		wifi_event_sta_disconnected_t *disconnect = (wifi_event_sta_disconnected_t *)event_data;
		ESP_LOGE(TAG, "Wifi Disconnect reason: %d", disconnect->reason);
		
	} else if (IP_EVENT == event_base && IP_EVENT_STA_GOT_IP == event_id) {
		ip_event_got_ip_t *ip = (ip_event_got_ip_t *)event_data;
		//ESP_LOGI(TAG, "Got Ip Address " "%d.%d.%d.%d", IP2STR(&ip->ip_info.ip));
		int_ip = ip->ip_info.ip.addr;
		local_ip[0] = *((unsigned char *)&int_ip);
		local_ip[1] = *((unsigned char *)&int_ip+1);
		local_ip[2] = *((unsigned char *)&int_ip+2);
		local_ip[3] = *((unsigned char *)&int_ip+3);

		ESP_LOGI(TAG, "Got Ip Address %d.%d.%d.%d\r\n", local_ip[0],local_ip[1],local_ip[2],local_ip[3]);
		
	}
	else if (event_id == WIFI_EVENT_AP_STACONNECTED) {
        wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
        ESP_LOGE(TAG, "station "MACSTR" join, AID=%d",
                 MAC2STR(event->mac), event->aid);
    } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
        wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
        ESP_LOGE(TAG, "station "MACSTR" leave, AID=%d",
                 MAC2STR(event->mac), event->aid);
    }
	else
	{
		ESP_LOGI(TAG, "--------Wifi Event %d", (int)event_id);
	}
}

static void initSoftApName(void)
{
    esp_err_t ret = esp_read_mac(base_wifi_mac_addr, ESP_MAC_WIFI_STA);

    memset(AP_NAME, 0, sizeof(AP_NAME));
    snprintf(AP_NAME, sizeof(AP_NAME), "DoNotWork-AP-%02X%02X", base_wifi_mac_addr[4], base_wifi_mac_addr[5]);

    ESP_LOGE(TAG, "ssid :%s", AP_NAME);
}

static void set_softap_ip(esp_netif_t *softap)
{
    // char temp[16] = {0};
    esp_netif_ip_info_t local_ip;
    memset(&local_ip, 0 , sizeof(esp_netif_ip_info_t));
    //snprintf(temp, sizeof(temp), "192.168.4.%d", base_wifi_mac_addr[5]);

    local_ip.ip.addr = ipaddr_addr(LogalAP);
    local_ip.gw.addr = ipaddr_addr(LogalAP);
    local_ip.netmask.addr = ipaddr_addr("255.255.255.0");
    
    esp_netif_dhcps_stop(softap);
    esp_netif_set_ip_info(softap, &local_ip);
    esp_netif_dhcps_start(softap);
    ESP_LOGI(TAG, "soft-ap IP: %s", LogalAP);
}


void initSoftAp(void)
{
    ESP_LOGE(TAG, "Into initSoftApStation");
    
    initSoftApName();
    if(softap == NULL)
    {
        softap = esp_netif_create_default_wifi_ap();
    }
    
	// esp_event_handler_instance_t any_id;
	// esp_event_handler_instance_t got_ip;
	// ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &softap_event_handler, NULL, &any_id));
	// ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &softap_event_handler, NULL, &got_ip));
    
    set_softap_ip(softap);

    wifi_config_t wifi_config ;
    strncpy((char*)wifi_config.ap.ssid, AP_NAME, strlen(AP_NAME));

    wifi_config.ap.max_connection = 4;
    wifi_config.ap.channel = 1;
    wifi_config.ap.ssid_len = strlen(AP_NAME);
    wifi_config.ap.authmode = WIFI_AUTH_OPEN;//WIFI_AUTH_WPA_WPA2_PSK;
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
	// tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, "TPV-eSign");
    ESP_ERROR_CHECK(esp_wifi_start());

    ESP_LOGE(TAG, "Soft ap ok");

    APWebServerStart();
}
